package com.hjk.common.utils.reflect;

import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author huangjunkai
 * @version 1.0
 * @date 2021/03/23
 */
@Slf4j
public class UnsafeUtils {

    @SneakyThrows
    public Unsafe getUnsafe() {
        Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);
        return (Unsafe) unsafeField.get(null);
    }

    @Test
    @SneakyThrows
    public void test34() {
        Unsafe unsafe =  getUnsafe();
        aaa aaa = new aaa();
        aaa bbb = new aaa();
        aaa.setName("你好");


        boolean swapObject = unsafe.compareAndSwapObject(aaa, 0, aaa, bbb);

        System.out.println(swapObject);
        System.out.println(bbb);
        System.out.println(aaa);
    }

    @Data
    class aaa{
        private String name;
    }

}
